<template>
	<view :class="config.style">
		<view class="sale-bar">
			<view class="item back" @click="back()">
				<view class="sale-icon">
					<uni-icons type="arrowthinleft" size="14" color="#999"></uni-icons> 
				</view>
				<view class="text">返回</view>
			</view>
			<view class="item favorites" @click="favorites()">
				<view class="sale-icon">
					<uni-icons type="star-filled" size="14" :color="favorites_yesno==1?'#f90':'#999'"></uni-icons> 
				</view>
				<view class="text">收藏</view>
			</view>
			<view class="item share" @click="share()">
				<view class="sale-icon">
					<uni-icons type="redo-filled" size="14" color="#999"></uni-icons> 
				</view>
				<view class="text">分享</view>
			</view>
			<view class="sale-btn">
				<view v-if="canUserMemberCard">
					<button v-if="type=='Live'" type="default" size="mini" class="sale-button" @click="joinLiveRoom()">进入直播间</button>
					<button v-else-if="type=='Material'" type="default" size="mini" class="sale-button" @click="materialDownload()">立即下载</button>
					<button v-else-if="type=='ExamPaper'" type="default" size="mini" class="sale-button" @click="examPaperTest()">开始答题</button>
					<button v-else type="default" size="mini" class="sale-button disabled" disabled="true">会员免费尊享</button>
				</view>
				
				<view v-else>
					<view v-if="data.purchase == 0">
						<view v-if="data.card_data">
							<template v-if="config.mobile_bind==2 && userInfo.memberInfo.mobile == ''">
								<!-- #ifdef MP -->
								<!--支持卡密兑换-->
								<button v-if="showSale" type="default" size="mini" class="sale-button" hover-class="button-hover" open-type="getPhoneNumber" @getphonenumber="decryptPhoneNumber">
									{{config.sale_btn.before}}
								</button>
								<!--支持卡密兑换-->
								<button v-else type="default" size="mini" class="sale-button" hover-class="button-hover" open-type="getPhoneNumber" @getphonenumber="decryptPhoneNumber">
									激活码兑换
								</button>
								<!-- #endif -->
								<!-- #ifdef H5 -->
								<!--支持卡密兑换-->
								<button v-if="showSale" type="default" size="mini" class="sale-button" hover-class="button-hover" @click="bindMobile()">
									{{config.sale_btn.before}}
								</button>
								<!--支持卡密兑换-->
								<button v-else type="default" size="mini" class="sale-button" hover-class="button-hover" @click="bindMobile()">
									激活码兑换
								</button>
								<!-- #endif -->
							</template>
							<template v-else>
								<!--支持卡密兑换-->
								<button v-if="showSale" type="default" size="mini" class="sale-button" hover-class="button-hover" @click="cardAndSale()">
									{{config.sale_btn.before}}
								</button>
								<!--支持卡密兑换-->
								<button v-else type="default" size="mini" class="sale-button" hover-class="button-hover" @click="confirmDialog()">
									激活码兑换
								</button>
							</template>
						</view>
						<view v-else>
							<!-- #ifdef MP -->
							<!--判断是否绑定手机号-->
							<template v-if="config.mobile_bind==2 && userInfo.memberInfo.mobile == ''">
								<button v-if="data.price == 0" type="default" size="mini" class="sale-button" hover-class="button-hover" open-type="getPhoneNumber" @getphonenumber="decryptPhoneNumber">
									免费领取
								</button>
								<template v-else>
									<!--下单支付-->
									<button v-if="showSale" type="default" size="mini" class="sale-button" hover-class="button-hover" open-type="getPhoneNumber" @getphonenumber="decryptPhoneNumber">
										{{config.sale_btn.before}}
									</button>
									<!--IOS禁用下单支付-->
									<button v-else type="default" size="mini" class="sale-button disabled" disabled="true">
										暂不可用
									</button>
									<view class="ios-tip" v-if="!showSale">
										由于相关规范，ios功能暂不可用
									</view>
								</template>
							</template>
							<!-- #endif -->
							
							<!-- #ifdef H5 -->
							<!--h5判断是否绑定手机号-->
							<template v-if="config.mobile_bind==2 && userInfo.memberInfo && userInfo.memberInfo.mobile==''">
								<button v-if="data.price == 0" type="default" size="mini" class="sale-button" hover-class="button-hover" @click="bindMobile()">
									免费领取
								</button>
								<template v-else>
									<!--下单支付-->
									<button type="default" size="mini" class="sale-button" hover-class="button-hover" @click="bindMobile()">
										{{config.sale_btn.before}}
									</button>
								</template>
							</template>
							<!-- #endif -->
							
							<template v-else>
								<button v-if="data.price == 0" type="default" size="mini" class="sale-button" hover-class="button-hover" @click="sale()">
									免费领取
								</button>
								<template v-else>
									<!--下单支付-->
									<button v-if="showSale" type="default" size="mini" class="sale-button" hover-class="button-hover" @click="sale()">
										{{config.sale_btn.before}}
									</button>
								
									<!--IOS禁用下单支付-->
									<button v-else type="default" size="mini" class="sale-button disabled" disabled="true">
										暂不可用
									</button>
									<view class="ios-tip" v-if="!showSale">
										由于相关规范，ios功能暂不可用
									</view>
								</template>
							</template>
						</view>
					</view>
					
					<view v-else>
						<!--已购买-->
						<view v-if="type == 'Knowledge'">
							<button v-if="data.column_sale==1" type="default" size="mini" class="sale-button disabled" disabled="true">专栏内已购</button>
							<button v-else type="default" size="mini" class="sale-button disabled" disabled="true">
								<text v-if="data.price == 0">领取成功</text>
								<text v-else>{{config.sale_btn.after}}</text>
							</button>
						</view>
						<button v-if="type=='KnowledgeColumn'" type="default" size="mini" class="sale-button disabled" disabled="true">
							<text v-if="data.price == 0">领取成功</text>
							<text v-else>{{config.sale_btn.after}}</text>
						</button>
						<button v-if="type=='Offline'"type="default" size="mini" class="sale-button disabled" disabled="true">
							<text v-if="data.price == 0">领取成功</text>
							<text v-else>{{config.sale_btn.after}}</text>
						</button>
						<button v-if="type=='Live'" type="default" size="mini" class="sale-button" @click="joinLiveRoom()">进入直播间</button>
						<button v-if="type=='Material'" type="default" size="mini" class="sale-button" @click="materialDownload()">立即下载</button>
						<button v-if="type=='ExamPaper'" type="default" size="mini" class="sale-button" @click="examPaperTest()">开始答题</button>
					</view>
				</view>
			</view>
		</view>
		
		<!-- 卡密兑换提交信息 -->
		<uni-popup id="dialogInput" ref="dialogInput" type="dialog" @change="change">
			<view class="cdkey-section">
				<view class="title">激活码</view>
				<view class="input-group">
					<view class="input-row">
						<input type="text" class="input-clear" maxlength="32" placeholder="请输入激活码" v-model="cdkey" />
					</view>
					<view class="submit">
						<button type="default" class="submit-cdkey" hover-class="button-hover" @click="dialogInputConfirm()"> 确 认 </button>
					</view>
				</view>
			</view>
		</uni-popup>
		
		<!-- 试卷答题模式选择 -->
		<uni-popup id="ExamPaperMode" ref="ExamPaperMode" type="bottom" @change="change">
			<view class="exam-paper-mode-section">
				<view class="item" v-if="data.test_support == 1" @click="examPaperModeJoin('test')">
					<text>模拟练习</text>
				</view>
				<view class="item" v-if="data.exam_support == 1" @click="examPaperModeJoin('exam')">
					<text>进入考试</text>
				</view>
				<view class="item" v-if="data.exam" @click="examPaperModeJoin('record')">
					<text>考试成绩查询</text>
				</view>
				<view class="item" v-if="data.test_support == 1" @click="examPaperModeJoin('wrong')">
					<text>错题本</text>
				</view>
				<view class="cannel" >
					<button @click="cancleExamPopup()">取消</button>
				</view>
			</view>
		</uni-popup>
		<!--绑定手机号-->
		<BindMobile ref="bindMobile" :config="config" @getUserInfo="getUserInfo"/>
		<!--登录框-->
		<Login ref="login" :config="config" @getUserInfo="getUserInfo" @getData="getData"/>
		<!--分享海报-->
		<QrcodePoster ref="poster" 
				:title="data.title" 
		        :subTitle="data.description" 
		        :headerImg="data.cover_400" 
				:abImg="config.cover_200" 
		        :price="data.price" />
	</view>
</template>

<script>
	
	//#ifdef MP
	// 小程序端执行
	import siteInfo from '../../../siteinfo.js'
	//#endif
	//#ifdef H5
	import siteInfo from '../../../siteinfoH5.js'
	//#endif
	import QrcodePoster from '../../../components/muu-qrcode-poster/muu-qrcode-poster.vue'
	import BindMobile from './bindMobile.vue'
	
	export default {
		name: 'SaleBar',
		components: {
			QrcodePoster,
			BindMobile
		},
		props: {
			config: {
				type: Object,
				default: {}
			},
			type: {
				type: String,
				default: ''
			},
			data: {
				type: Object,
				default: {}
			}
		},
		data() {
			return {
				userInfo: {},
				canUserMemberCard: false, //是否会员免费尊享
				purchase: 0, // 是否已购买
				favorites_yesno: 0, //是否已收藏
				qrcode: '', // 小程序二维码
				showSale: true, //IOS端是否显示购买按钮
				cdkey: ''
			}
		},
		created() {
			this.favorites_yesno = this.data.favorites_yesno
			this.purchase = this.data.purchase
			//设置会员免费尊享
			this.canUserMemberCardFn()
			//获取已登录用户数据
			this.getUserInfo()
			//ISO限制处理
			this.iosShowSale()
			this.$nextTick(() => {
				
			})
			
		},
		methods: {
			
			/**
			 * IOS端支付限制判断
			 */
			iosShowSale: function(){
				this.showSale = this.util.setShowSale()
				console.log(this.data.price)
				if(this.data.price == 0 || this.type == 'Offline'){
					this.showSale = true
				}
			},
			
			/**
			 * 设置会员免费尊享
			 */
			canUserMemberCardFn: function(){
				if(this.data.can_be_used_card && this.data.can_be_used_card['member_card_info']['discount'] == 0){
					//会员专享
					this.canUserMemberCard = true
				}
			},
			
			/**
			 * 返回
			 */
			back: function(){
				let pages = getCurrentPages() // 获取栈实例
				let prevPage = pages[pages.length - 2]
				if(prevPage !== undefined){
					uni.navigateBack()
				}else{
					let url = '../index/index'
					uni.redirectTo({
						url: url
					});
				}
			},
			
			favorites: function(){
				//console.log(this.data.id)
				let that = this
				let params = this.urlParams
				let data = {}
					data.action = 'edit'
					data.m = 'muu_classroom'
					data.info_id = this.data.id
					data.info_type = this.type
				//获取数据

				let url = '';
					//#ifdef MP-WEIXIN
					url = 'entry/wxapp/favorites'
					//#endif
					//#ifdef H5
					url = 'entry/site/favorites'
					//#endif
				this.util.request({
					url : url,
					data : data,
					method: 'POST',
					
					success: function (res) {
						if (res.data.code == 200) {
							//console.log(res)
							uni.showToast({
								title: res.data.msg,
								duration: 2000
							});
							that.setFavorites()
						}
						if (res.data.code == 0 && res.data.data == 'login') {
							//触发登录框
							that.$refs.login.loginDialog()
						}
					}
				})
				
			},
			/**
			 * 设置收藏
			 */
			setFavorites: function(){
				if(this.favorites_yesno == 0){
					this.favorites_yesno = 1
				}else{
					this.favorites_yesno = 0
				}
			},
			
			/**
			 * 分享海报
			 */
			share() {
				let that = this
				// #ifdef MP-WEIXIN
				//获取二维码base64码
				let qrcode_url = this.util.url('entry/wxapp/qrcode',{
					'type': this.type,
					'type_id': this.data.id,
					'm': 'muu_classroom'
				})
				that.$refs.poster.showCanvas(qrcode_url)
				// #endif
				
				// #ifdef H5
				// console.log(that.type)
				let urlParams = 'type_id='+that.data.id+'&type='+that.type
				let url = '/muu_classroom/pages/share/index?' + urlParams
				
				//指定跳转地址
				uni.navigateTo({
				    url: url
				});
				// #endif
			},
			
			/**
			 * 下单支付
			 */
			 sale: function(){
				let that = this

				//提交数据
				let data = {
					'order_info_id' : this.data.id,
					'order_info_type' : this.type,
					'm' : 'muu_classroom',
					'action': 'create',
				}
				//console.log(data)
				//获取列表数据
				let url = '';
					//#ifdef MP-WEIXIN
					url = 'entry/wxapp/orders'
					//#endif
					//#ifdef H5
					url = 'entry/site/orders'
					//#endif
				//下单
				this.util.request({
					url : url,
					data: data,
					method: 'POST',
					
					success: function (res) {
						//console.log(res.data);
						if (res.data.code == 200) {
							
							if(res.data.data.paid == 0){
								uni.showToast({
								    title: '订单已创建，正在跳转支付',
								    duration: 2000,
									icon: 'none'
								});
								that.pay('muu_classroom',res.data.data.order_no)
							}else{
								uni.showModal({
									title: '温馨提示',
									content: '购买成功',
									showCancel: false,
									success: function(){
										//购买成功后，执行父组件获取数据方法（更新数据）
										that.$emit('getData')
									}
								});
							}
						}else if(res.data.code == 0 && res.data.data == 'login') {
							//触发登录框
							that.$refs.login.loginDialog()
						}else if(res.data.code == 0 && res.data.data == 'mobile_bind') {
							//触发绑定手机号
							uni.showToast({
								title: '为了更好的为您提供服务，请绑定手机号！',
								icon: 'none',
								duration: 2000
							});
							
						}else{
							uni.showToast({
								title: res.data.msg,
								icon: 'none',
								duration: 2000
							});
						}
					}
				})
			},
			
			/**
			 * 非小程序端绑定手机号
			 */
			bindMobile: function(){
				this.$refs.bindMobile.bindMobileOpen()
			},

			/**
			 * 支持卡密兑换购买方式
			 */
			cardAndSale: function(){
				let that = this
				//console.log('支持购买及卡密激活')
				uni.showActionSheet({
					itemList: ['激活码兑换', '立即购买'],
					success: function (res) {
						if(res.tapIndex == 0){
							// 打开卡密激活popup弹出框
							that.confirmDialog()
						}
						if(res.tapIndex == 1){
							that.sale()
						}
					},
					fail: function (res) {
						console.log(res.errMsg);
					}
				});
			},
			
			/**
			 * 执行父级getData方法
			 */
			getData() {
				// 更新父组件数据
				this.$emit('getData')
			},
			
			/**
			 * 订单支付
			 */
			pay: function(module,order_no){
				let that = this
				// 支付
				let data = {}
					data.module = module
					data.order_no = order_no
				let url = ''
					// #ifdef MP-WEIXIN
					url = 'entry/wxapp/pay'
					data.first = 0;
					// #endif
					// #ifdef H5
					url = 'entry/site/pay'
					data.action = 'jssdk'
					data.first = 0;
					// #endif
				this.util.request({
					url : url,
					data : data,
					
					success(res) {
						if (res.data && res.data.data && res.data.code == 200) {
							console.log(res);
							uni.hideToast();
							// #ifdef H5
							//JSSDK
							that.wechat.pay({
								'timeStamp': res.data.data.timeStamp,
								'nonceStr': res.data.data.nonceStr,
								'package': res.data.data.package,
								'signType': res.data.data.signType,
								'paySign': res.data.data.paySign,
								'success': function (res) {
									//执行支付成功提示
									uni.showModal({
									    title: '提示',
									    content: '支付成功',
										confirmText: '查看订单',
										cancleText: '返回课程',
									    success: function (res) {
									        if (res.confirm) {
									            console.log('用户点击确定');
												let order_url = '../my/orders/index'
												uni.redirectTo({
													url: order_url
												});
									        } else if (res.cancel) {
									            console.log('用户点击取消');
												uni.navigateBack()
									        }
									    }
									});
								},
								'fail': function (res) {
									uni.navigateBack()
								}
							},window.location.href);
							// #endif
							
							// #ifdef MP-WEIXIN
							//发起支付
							uni.requestPayment({
								'provider': 'wxpay',
								'timeStamp': res.data.data.timeStamp,
								'nonceStr': res.data.data.nonceStr,
								'package': res.data.data.package,
								'signType': res.data.data.signType,
								'paySign': res.data.data.paySign,
								'success': function (res) {
									//执行支付成功提示
									// 更新父组件数据
									that.$emit('getData')
								},
								'fail': function (res) {
									
								}
							})
							// #endif
							
						 }else{
							uni.showModal({
								title: '系统提示',
								content: res.data.msg ? res.data.msg : '错误',
								showCancel: false,
								success: function (res) {
									if (res.confirm) {
										uni.navigateBack()
									}
								}
							})
						}
					},
					fail(res) {
					  uni.showModal({
						title: '系统提示',
						content: res.data.msg ? res.data.msg : '错误',
						showCancel: false,
						success: function (res) {
							if (res.confirm) {
								
							}
						}
					  })
					}
				});
			},
			
			
			/**
			 * 解密小程序端手机号码
			 * @param {Object} e
			 */
			decryptPhoneNumber: function(e) {
				let that = this
				console.log(e);    
				if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {    
					console.log('用户拒绝提供手机号');  
				}else{
					console.log('用户同意提供手机号');
					// 小程序端重新获取session_key
					uni.login({
						success: function (res) {
							console.log(res)
							let code = res.code
							that.util.request({
								url: 'entry/wxapp/session',
								data: {
									'action': 'openid',
									'code': code ? code : '',
								},
								success: function (res) {
									console.log(res);
									if (res.data.code == 200) {
										if(res.data.data.openid){
											uni.setStorageSync('openid', res.data.data.openid)
										}
										// 请求绑定手机号
										let encryptedData = e.detail.encryptedData;
										let iv = e.detail.iv;
										let data = {}
											data.action = 'decrypt_mobile'
											data.m = 'muu_classroom'
											data.iv = iv
											data.encryptedData = encryptedData
										//动态获取用户数据
										let url = ''
											//#ifdef MP-WEIXIN
											url = 'entry/wxapp/ucenter'
											//#endif
											//#ifdef H5
											url = 'entry/site/ucenter'
											//#endif
										that.util.request({
											url : url,
											data : data,
											success: function (res) {
												if (res.data.code == 200) {
													console.log(res.data)
													that.getApiUserInfo()
												}
												uni.showToast({
												    title: res.data.msg,
												    duration: 2000,
													icon: 'none'
												});
											}
										})
									}else{
										uni.showToast({
										    title: res.data.msg,
										    duration: 2000,
											icon: 'none'
										});
									}
								}
							});
						},
					})
				}
			},
			
			/**
			 * 打开卡密激活和购买选择popup弹出框
			 */
			confirmDialog: function(){
				this.$refs.dialogInput.open()
			},
			
			/**
			 * 卡密激活输入对话框的确定事件
			 */
			dialogInputConfirm() {
				let that = this
				let data = {}
					data.id = that.data.id
					data.type = that.type
					data.cdkey = that.cdkey
				// 激活提交
				let url = ''
					// #ifdef MP-WEIXIN
					url = 'entry/wxapp/card'
					data.action = 'kcard_activate'
					data.m = 'muu_classroom'
					// #endif
					
					// #ifdef H5
					url = 'entry/site/kcdkey'
					data.action = 'activate'
					data.m = 'muu_classroom_plugin_card'
					// #endif
				this.util.request({
					url : url,
					data: data,
					method: 'POST',
					
					success : function(res) {
						//console.log(res)
						if(res.data.code == 200){
							// 激活成功，更新数据
							uni.showToast({
							    title: res.data.msg,
								icon: 'none',
								position: 'bottom',
							    duration: 2000
							});
							// 更新父组件数据
							that.$emit('getData')
							that.$refs.dialogInput.close()
						}
						if(res.data.code == 0){
							// 激活失败提示
							uni.showToast({
							    title: res.data.msg,
								icon: 'none',
								position: 'bottom',
							    duration: 2000
							});
						}
						if(res.data.code == 0 && res.data.data == 'login') {
							//触发登录框
							that.$refs.login.loginDialog()
						}
					},
					fail : function(res){
						uni.showModal({
						    title: '提示',
						    content: '发生错误，请稍后再试~',
						    success: function (res) {
						        if (res.confirm) {
						            console.log('用户点击确定');
						        } else if (res.cancel) {
						            console.log('用户点击取消');
						        }
						    }
						});
					},
				})
			},
			
			/**
			 * 进入直播间
			 */
			joinLiveRoom() {
				let that = this
			   	let base_url = siteInfo.siteroot + '?i=' + this.data.uniacid + '&c=entry&m=muu_classroom_plugin_livecourse'
				// #ifdef MP
				//小程序内webview参数
			   	let params = {
			   		type : 'live',
			   		web_url : encodeURIComponent(base_url  + '&do=live&action=room&roomid=' + this.data.id +'&uid='+this.userInfo.memberInfo.uid+'&source=miniprogram&uniacid='+this.data.uniacid)
			   	};
			   	console.log(params)
			   	let paramsStr = JSON.stringify(params);//参数传递给小程序，所以这里需要转为字符串
			   	
			   	let url = '../detail/index?params=' + paramsStr
			   	//console.log(url)
			   	//指定跳转地址
			   	uni.navigateTo({
			   	    url: url
			   	});
				// #endif
				
				// #ifdef H5
				let url = base_url  + '&do=live&action=room&roomid=' + this.data.id
				console.log('liveroom',url)
				window.location.href = url
				// #endif
			},
			
			/**
			 * 资料下载文件至本地
			 */
			materialDownload() {
				let that = this
				
				let url = ''
				let data = {
					action: 'limit'
				}
				//判断下载限制
				// #ifdef H5
				url = 'entry/site/material'
				data.m = 'muu_classroom_plugin_material'
				// #endif
				
				// #ifdef MP-WEIXIN
				url = 'entry/wxapp/material'
				data.m = 'muu_classroom'
				// #endif
				
				this.util.request({
					url : url,
					data: data,
					success : function(res) {
						console.log(res)
						if(res.data.code == 0){
							uni.showToast({
								icon: 'none',
								title: res.data.msg
							})
							return false
						}
						if(res.data.code == 200){
							that.startDownload()
						}
					},
				})
			},
			
			/**
			 * 开始资料下载
			 */
			startDownload() {
				let that = this
				// 进入下载逻辑
				let url = ''
				let data = {
					id: this.data.id,
					action: 'sign'
				}
				// #ifdef H5
					url = 'entry/site/material'
					data.m = 'muu_classroom_plugin_material'
				// #endif
				// #ifdef MP-WEIXIN
					url = 'entry/wxapp/material'
					data.m = 'muu_classroom'
				// #endif
				this.util.request({
					url : url,
					data: data,
					success : function(res) {
						//console.log(res)
						if(res.data.code == 200){
							let sign = res.data.data
							
							// #ifdef H5
							// 跳转页面
							let jump_url = '../material/download?sign=' + sign + '&id=' + that.data.id
							uni.navigateTo({
								url: jump_url
							})
							// #endif
							
							// #ifdef MP
							//下载资料
							uni.showLoading({
							    title: '下载中，请稍后'
							});
							let url = 'entry/wxapp/material'
							let data = {
								'm': 'muu_classroom',
								'sign' : sign,
								'action' : 'download'
							}
							//console.log(data)
							//获取数据
							that.util.request({
								url : url,
								data : data,
								success: function (res) {
									if (res.data.code == 200) {
										//console.log(res.data)
										let downloadTask = uni.downloadFile({
										    url: res.data.data.material_url, //仅为示例，并非真实的资源
											
										    success: (res) => {
										        if (res.statusCode === 200) {
													//console.log(res)
													//console.log(`${wx.env.USER_DATA_PATH}` + "/" + that.data.filename)
													uni.getFileSystemManager().saveFile({
														tempFilePath: res.tempFilePath,
														filePath: `${wx.env.USER_DATA_PATH}` + "/" + that.data.filename,
													})
													
													uni.showModal({
														title: '是否打开文件',
														content: '',
														success(ans) {
															if (ans.confirm) {
																uni.openDocument({
																	filePath: `${wx.env.USER_DATA_PATH}` + "/" + that.data.filename,
																	showMenu: true,
																	success: function (ress) {
																		console.log('打开文档成功');
																	},
																})
															}
														}
													})
										        }
										    }
										});
										
										downloadTask.onProgressUpdate((res) => {
										    console.log('下载进度' + res.progress);
										    console.log('已经下载的数据长度' + res.totalBytesWritten);
											if(res.progress == 100){
												uni.hideLoading();
											}
										    //console.log('预期需要下载的数据总长度' + res.totalBytesExpectedToWrite);
										
										    // 测试条件，取消下载任务。
										    if (res.progress > 50) {
										        //downloadTask.abort();
										    }
										});
									}
									if (res.data.code == 0) {
										uni.showModal({
										    title: '提示',
										    content: res.data.msg,
											showCancel: false,
										    success: function (res) {
										        if (res.confirm) {
										            console.log('用户点击确定');
													uni.navigateBack({
													    delta: 1
													});
										        }
										    }
										});
									}
								}
							})
							// #endif
						}
					},
					fail : function(res){
				
					},
				})
			},
			
			/**
			 * 题库考试，开始答题方法
			 */
			examPaperTest(){
				this.$refs.ExamPaperMode.open()
			},
			cancleExamPopup(){
				this.$refs.ExamPaperMode.close()
			},
			examPaperModeJoin(mode){
				let that = this
				console.log(mode)
				let url = ''
				//指定跳转地址
				let urlParams = 'id='+this.data.id
				if(mode == 'test'){
					urlParams += '&mode=test'
					url = 'test?' + urlParams
				}
				if(mode == 'exam'){
					urlParams += '&mode=exam'
					url = 'test?' + urlParams
				}
				if(mode == 'record'){
					url = 'record?' + urlParams
				}
				if(mode == 'wrong'){
					url = 'wrong?' + urlParams
				}
				
				console.log(url)
				uni.navigateTo({
				    url: url
				});
			},
			
			/**
			 * 获取已登录用户数据
			 */
			getUserInfo: function(){
				let that = this
				let userInfo = uni.getStorageSync('userInfo') || {}
				console.log('userInfo',userInfo)
				if(Object.keys(userInfo).length > 0){
					this.userInfo = userInfo
				}else{
					this.util.getUserInfo((res)=>{
						that.userInfo = res
					})
				}
			},
			
			/**
			 * 获取用户详情数据
			 */
			getApiUserInfo: function(){
				let that = this
				let data = {}
					data.action = 'user_info'
					data.m = 'muu_classroom'
				//动态获取用户数据
				let url = ''
					//#ifdef MP-WEIXIN
					url = 'entry/wxapp/ucenter'
					//#endif
					//#ifdef H5
					url = 'entry/site/ucenter'
					//#endif
				this.util.request({
					url : url,
					data : data,
					success: function (res) {
						if (res.data.code == 200) {
							that.userInfo.sessionid = that.userInfo.sessionid
							that.userInfo.memberInfo = res.data.data
							uni.setStorageSync('userInfo', that.userInfo)
							//console.log(that.userInfo)
						}
						//结束下拉刷新
						uni.stopPullDownRefresh();
					}
				})
			},
			
			/**
			 * popup 状态发生变化触发
			 * @param {Object} e
			 */
			change: function(e) {
				console.log('popup ' + e.type + ' 状态', e.show)
			},
		}
	}
</script>

<style lang="scss">
	.ios-tip {
		height: 30rpx;
		font-size: 20rpx;
		line-height: 30rpx;
		text-align: center;
		color: #fff;
		background-color: #ff9900;
		position: fixed;
		top: auto;
		right: 0;
		left: 0;
		bottom: 166rpx;
	}
	.sale-bar {
		display: flex;
		position: fixed;
		top: auto;
		right: 0;
		left: 0;
		bottom: 0;
		background-color: #ffffff;
		text-align: center;
		border-top: 2rpx solid #f4f6f7;
		padding-bottom: calc(constant(safe-area-inset-bottom));
		padding-bottom: calc(env(safe-area-inset-bottom));
		>.item {
			padding: 0;
			flex: 1;
			.sale-icon {
				display: block;
				height: 56rpx;
				overflow: hidden;
				line-height: 56rpx;
			}
			.text {
				display: block;
				font-size: 24rpx;
				color: #999;
				height: 40rpx;
				line-height: 40rpx;
				overflow: hidden;
			}
		}
		.sale-btn {
			width: 360rpx;
			padding: 0;
			padding: 10rpx 10rpx 10rpx 0;
			
			.sale-button {
				display: block;
				font-size: 30rpx;
				padding: 0;
				line-height: 77rpx;
				border-radius: 30rpx;
				color: #fff;
			}
			.disabled {
				opacity: .6;
			}
		}
		
	}
	
	/*试卷答题模式选择样式*/
	.exam-paper-mode-section {
		background-color: #fff;
		padding: 20rpx;
		border-radius: 10rpx 10rpx 0 0;
		.item {
			padding: 20rpx;
			margin: 10rpx 0;
			text-align: center;
			&:active {
				background-color: #f8f8f8;
			}
		}
		.cannel {
			padding: 20rpx 20rpx 0 20rpx;
			text-align: center;
			button {
				color: #999;
				&:after {
					border: 0;
				}
			}
		}
	}
	
	.cdkey-section {
		padding: 20rpx 0;
		background-color: #fff;
		border-radius: 10rpx;
		width: 500rpx;
		.title {
			font-size: 36rpx;
			font-weight: 700;
		    text-align: center;
			margin-bottom: 10rpx;
		}
		.input-group {
			padding: 20rpx;
			.input-row {
				padding: 10rpx 0;
				.input-clear {
					flex: 1;
					height: 76rpx;
					line-height: 76rpx;
				}
			}
			.submit {
				padding: 20rpx 0 0 0;
				button {
					color: #fff;
					&:after {
						border: 0;
					}
				}
			}
		}
	}
	
	/*页面风格设计*/
	.Blue {
		.sale-bar {
			.sale-btn {
				.sale-button {
					background-color: $Blue;
				}
				.button-hover {
					background-color: $Blue-hover;
				}
			}
		}
		.cdkey-section {
			.submit {
				button {
					background-color: $Blue;
				}
			}
		}
	}
	.Green {
		.sale-bar {
			.sale-btn {
				.sale-button {
					background-color: $Green;
				}
				.button-hover {
					background-color: $Green-hover;
				}
			}
		}
		.cdkey-section {
			.submit {
				button {
					background-color: $Green;
				}
			}
		}
	}
	.LightRed {
		.sale-bar {
			.sale-btn {
				.sale-button {
					background-color: $LightRed;
				}
				.button-hover {
					background-color: $LightRed-hover;
				}
			}
		}
		.cdkey-section {
			.submit {
				button {
					background-color: $LightRed;
				}
			}
		}
	}
	.Orange {
		.sale-bar {
			.sale-btn {
				.sale-button {
					background-color: $Orange;
				}
				.button-hover {
					background-color: $Orange-hover;
				}
			}
		}
		.cdkey-section {
			.submit {
				button {
					background-color: $Orange;
				}
			}
		}
	}
	.LightPink {
		.sale-bar {
			.sale-btn {
				.sale-button {
					background-color: $LightPink;
				}
				.button-hover {
					background-color: $LightPink-hover;
				}
			}
		}
		.cdkey-section {
			.submit {
				button {
					background-color: $LightPink;
				}
			}
		}
	}
	.Magenta {
		.sale-bar {
			.sale-btn {
				.sale-button {
					background-color: $Magenta;
				}
				.button-hover {
					background-color: $Magenta-hover;
				}
			}
		}
		.cdkey-section {
			.submit {
				button {
					background-color: $Magenta;
				}
			}
		}
	}
</style>
